package com.wgz.flag.service.impl;

import com.wgz.flag.entity.PrivateMessage;
import com.wgz.flag.mapper.PrivateMessageMapper;
import com.wgz.flag.service.PrivateMessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class PrivateMessageServiceImpl implements PrivateMessageService {
    private final PrivateMessageMapper privateMessageMapper;
//    private final MessageRecordMapper messageRecordMapper;

    @Override
    public List<PrivateMessage> getHistoryMessages(Long userId, Long friendId) {
        return privateMessageMapper.getHistoryMessages(userId, friendId, 100);
    }

    @Override
    public void sendMessage(PrivateMessage message) {
        // 如果是文件消息，确保文件名和大小被正确设置
        if (message.getMessageType() == 3) {  // 假设 2 是文件消息类型
            if (message.getFileName() == null) {
                // 如果没有文件名，从 content 中提取
                String content = message.getContent();
                String fileName = content.substring(content.lastIndexOf('/') + 1);
                message.setFileName(fileName);
            }
        }
        privateMessageMapper.insertMessage(message);
    }

    @Override
    public void markAsRead(Long userId, Long friendId) {
        privateMessageMapper.markAsRead(userId, friendId);
//        messageRecordMapper.clearUnreadCount(userId, friendId);
    }
}